package grape.attend.service.user.impl;

import grape.attend.service.group.api.IGroupClasseRelService;
import grape.attend.service.user.dto.GroupAndClasseDto;
import grape.attend.service.user.po.UserClockDate;
import grape.attend.service.user.po.UserClockDateClasse;
import grape.attend.service.user.mapper.UserClockDateClasseMapper;
import grape.attend.service.user.api.IUserClockDateClasseService;
import grape.common.exception.runtime.RBaseException;
import grape.common.service.common.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户打卡日期班次表 服务实现类
 * </p>
 *
 * @author yangwei
 * @since 2020-05-25
 */
@Service
public class UserClockDateClasseServiceImpl extends BaseServiceImpl<UserClockDateClasseMapper, UserClockDateClasse> implements IUserClockDateClasseService {


    @Autowired
    private IGroupClasseRelService iGroupClasseRelService;

    @Override
    public List<UserClockDateClasse> addUserClockDateClassesIfNecessary(String userClockDateId,String userId) {
        assertParamNotEmpty(userClockDateId,"userClockDateId 不能为空");
        assertParamNotEmpty(userId,"userId 不能为空");
        List<UserClockDateClasse> list = getByUserClockDateId(userClockDateId);
        if (!isEmpty(list)) {
            return list;
        }
        List<GroupAndClasseDto> groupAndClasseDtos = iGroupClasseRelService.getGroupAndClasseCurrentByUserId(userId);
        if (isEmpty(groupAndClasseDtos)) {
            throw new RBaseException("该用户没有分配班次");
        }
        UserClockDateClasse userClockDateClasseToBeInsert = null;
        List<UserClockDateClasse> insertList = new ArrayList<>(groupAndClasseDtos.size());
        for (GroupAndClasseDto groupAndClasseDto : groupAndClasseDtos) {
            userClockDateClasseToBeInsert = new UserClockDateClasse();
            userClockDateClasseToBeInsert.setUserClockDateId(userClockDateId);
            userClockDateClasseToBeInsert.setClasseId(groupAndClasseDto.getClasse().getId());
            userClockDateClasseToBeInsert.setGroupId(groupAndClasseDto.getGroup().getId());
            // 插入
            userClockDateClasseToBeInsert = insert(userClockDateClasseToBeInsert);
            insertList.add(userClockDateClasseToBeInsert);
        }
        return insertList;
    }
}
